package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocPlanDispatchDTO;
import org.jsola.hr.dto.SocPlanDispatchUserAddDTO;
import org.jsola.hr.dto.SocPlanDispatchUserUpdateDTO;
import org.jsola.hr.query.SocPlanDispatchUserQuery;
import org.jsola.hr.service.ISocPlanDispatchUserService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 增减员计划派单人员表
 *
 * @author zhr
 */
@RestController("hrSocPlanDispatchUserController")
@RequestMapping(HrConstants.API_PATH + "/v1/socPlanDispatchUsers")
public class SocPlanDispatchUserController implements ISocPlanDispatchUserControllerAPI {

    @Autowired
    private ISocPlanDispatchUserService socPlanDispatchUserService;

    @Override
    @PostMapping
    public Result<SocPlanDispatchUserVO> save(@Valid @RequestBody SocPlanDispatchUserAddDTO socPlanDispatchUserAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增增减员计划派单人员表权限校验

        return Result.success(
                socPlanDispatchUserService.save(socPlanDispatchUserAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocPlanDispatchUserUpdateDTO socPlanDispatchUserUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改增减员计划派单人员表权限校验

        int count = socPlanDispatchUserService.update(socPlanDispatchUserUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{socPlanDispatchUserId}")
    public Result<Integer> delete(@PathVariable String socPlanDispatchUserId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除增减员计划派单人员表权限校验

        int count = socPlanDispatchUserService.deleteByIds(tokenUser, socPlanDispatchUserId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socPlanDispatchUserIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除增减员计划派单人员表权限校验

        int count = socPlanDispatchUserService.deleteByIds(tokenUser, socPlanDispatchUserIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socPlanDispatchUserId}")
    public Result<SocPlanDispatchUserVO> findById(@PathVariable String socPlanDispatchUserId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看增减员计划派单人员表权限校验

        return Result.success(
                socPlanDispatchUserService.selectById(socPlanDispatchUserId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocPlanDispatchUserListVO>> listAll(@Valid SocPlanDispatchUserQuery socPlanDispatchUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看增减员计划派单人员表权限校验

        return Result.success(
                socPlanDispatchUserService.select(socPlanDispatchUserQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocPlanDispatchUserListVO>> page(@Valid SocPlanDispatchUserQuery socPlanDispatchUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socPlanDispatchUserService.selectPage(socPlanDispatchUserQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/dispatch")
    public Result<String> dispatch(@RequestBody SocPlanDispatchDTO socPlanDispatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        socPlanDispatchUserService.dispatch(socPlanDispatchDTO, tokenUser);
        return Result.success();
    }

    @Override
    @GetMapping("/taskOrderUser")
    public Result<Page<EmpSocPlanListVO>> taskOrderUser(@Valid SocPlanDispatchUserQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socPlanDispatchUserService.taskOrderUser(query, tokenUser)
        );
    }

    @Override
    @GetMapping("/taskOrderUserCount")
    public Result<TaskOrderUserCountVO> taskOrderUserCount(@Valid SocPlanDispatchUserQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socPlanDispatchUserService.taskOrderUserCount(query, tokenUser)
        );
    }

    @PostMapping("/socOverviewItemStats")
    public Result<SocOverviewItemStatsListVO> socOverviewItemStats(@RequestBody SocPlanDispatchUserQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socPlanDispatchUserService.socOverviewItemStats(query, tokenUser)
        );
    }

    @PostMapping("/socOverviewAmountStats")
    public Result<SocOverviewAmountStatsVO> socOverviewAmountStats(@RequestBody SocPlanDispatchUserQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socPlanDispatchUserService.socOverviewAmountStats(query, tokenUser)
        );
    }

    @PostMapping("/socOverviewListStats")
    public Result<Page<EmpSocInfoListVO>> socOverviewListStats(@RequestBody SocPlanDispatchUserQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socPlanDispatchUserService.socOverviewListStats(query, tokenUser)
        );
    }

    @PostMapping("/saveSocOverviewListUserHead")
    public Result<String> saveSocOverviewListUserHead(@RequestBody List<CompoundHeadVO> saveHead) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        socPlanDispatchUserService.saveSocOverviewListUserHead(saveHead, tokenUser);
        return Result.success();
    }

    @GetMapping("/socOverviewListHead")
    public Result<List<CompoundHeadVO>> socOverviewListHead() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socPlanDispatchUserService.socOverviewListHead(tokenUser)
        );
    }

    @PostMapping("/exportSocOverviewList")
    public ResponseEntity<Resource> exportSocOverviewList(@RequestBody SocPlanDispatchUserQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        String excelFilePath = socPlanDispatchUserService.exportSocOverviewList(query, tokenUser);
        String filenames = FileKit.getNameAndEncode(excelFilePath);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }
}

